/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ania2;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author pawel
 */
public class GameManager {

    private List<Move> moves;
    private ListIterator<Move> movesIterator;
    private Move first;
    private boolean whiteBegins;

    public GameManager() {
        moves = new LinkedList<Move>();
        movesIterator = moves.listIterator();
        first = new Move(getStandardBeginingPosition(), null, null);
        movesIterator.add(first);
        whiteBegins = true;
    }

    public GameManager(BoardPosition beginingPosition) {
        moves = new LinkedList<Move>();
        movesIterator = moves.listIterator();
        first = new Move(beginingPosition, null, null);
        movesIterator.add(first);
        whiteBegins = true;
    }

    public Move nextMove() {
        if (movesIterator.hasNext()) {
            return movesIterator.next();
        }
        return null;
    }

    public Move previousMove() {
        if (movesIterator.hasPrevious()) {
            movesIterator.previous();
            if (movesIterator.hasPrevious()) {
                movesIterator.previous();
                return movesIterator.next();
            }
        }
        return null;
    }

    public Move rewindForward() {
        Move m = null;
        while (movesIterator.hasNext()) {
            m = movesIterator.next();
        }
        return m;
    }

    public Move rewindBackward() {
        while (movesIterator.hasPrevious()) {
            movesIterator.previous();
        }
        return movesIterator.next();
    }

    public void addMove(Move move) {
        if (movesIterator.hasNext()) {
            //TODO dodać potwierdzenie nadpisania bieżącej kontynuacji partii
            removeSubsequentMoves();
        }
        movesIterator.add(move);
    }

    public void removeMove() {
        movesIterator.remove();
        if (movesIterator.hasNext()) {
            movesIterator.next();
        } else if (movesIterator.hasPrevious()) {
            movesIterator.previous();
            movesIterator.next();
        }
    }

    private void removeSubsequentMoves() {
        while (movesIterator.hasNext()) {
            movesIterator.next();
            movesIterator.remove();
        }
        if(movesIterator.hasPrevious()) {
            movesIterator.previous();
            movesIterator.next();
        }
    }

    private BoardPosition getStandardBeginingPosition() {
        long white = 0;
        for(int i=5; i<9; i++)
            white |= (1L<<(63-i));
        for(int i=10; i<18; i++)
            white |= (1L<<(63-i));
        long black = 0;
        for(int i=28; i<36; i++)
            black |= (1L<<(63-i));
        for(int i=37; i<41; i++)
            black |= (1L<<(63-i));
        return new BoardPosition(white, black, 0);
    }

    public boolean isWhiteTurn() {
        int index = movesIterator.nextIndex();
        boolean firstTurn = ((index%2)==0) ? false : true;
        return whiteBegins ? firstTurn : !firstTurn;
    }

    public boolean isEndOfMovesList() {
        return !movesIterator.hasNext();
    }

    public List<Move> getMovesList() {
        return moves;
    }
}
